/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.sistemas.sse.dao.impl;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import pe.edu.sistemas.sse.dao.IUbigeoDepartamentoDao;
import pe.edu.sistemas.sse.entity.UbigeoDepartamento;
import pe.edu.sistemas.sse.util.HibernateUtil;

/**
 *
 * @author laptop-w8
 */
@Repository("UbigeoDepartamentoDao")
public class UbigeoDepartamentoDao implements IUbigeoDepartamentoDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean agregarListaDepartamento(List<UbigeoDepartamento> departamentos) {
        boolean success = false;
        Session session = sessionFactory.openSession();

        try {
            session.beginTransaction();
            for (UbigeoDepartamento u : departamentos) {
                session.persist(u);
            }
            session.flush();
            success = true;
        } catch (HibernateException he) {
            session.getTransaction().rollback();
            throw he;
        } finally {
            session.getTransaction().commit();
            session.close();
        }

        return success;
    }

    @Override
    public List<UbigeoDepartamento> obtenerListaDepartamentos() {
        List<UbigeoDepartamento> l = null;
        Session session = sessionFactory.openSession();

        try {
            session.beginTransaction();
            l = session.createQuery("FROM " + UbigeoDepartamento.class.getSimpleName()).list();
            session.flush();
        } catch (HibernateException he) {
            session.getTransaction().rollback();
            throw he;
        } finally {
            session.getTransaction().commit();
            session.close();
        }

        return l;
    }

    @Override
    public boolean agregarDepartamento(UbigeoDepartamento departamento) {
        return HibernateUtil.agregarEntidad(sessionFactory, departamento);
    }

}
